package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.IOrder;
import model.IRouts;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.OrderService;
import service.RoutsService;
import service.ServiceManager;
import form.AdminConsolForm;

public class AdminConsolAction extends ConnectionAwareAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			AdminConsolForm input = (AdminConsolForm) form;  
			String date1 = "";
			String date2 = "";
			String date3 = "";
			int[] id_check_order = input.getId_check_order();
			int id_routs = input.getRouts_name();
			
				if (input.getDate1() != null){
					date1 = Escape.escapeHTML(input.getDate1());
				}
				if (input.getDate2() != null){
					date2 = Escape.escapeHTML(input.getDate2());
				}
				if (input.getDate3() != null){
					date3 = Escape.escapeHTML(input.getDate3());
				}
				
			OrderService orderService = ServiceManager.getDefault().getOrderService();
			RoutsService routsService = ServiceManager.getDefault().getRoutsService();
			
			if (request.getParameter("Show2") !=null){
					
					List<IOrder> orders = orderService.getOrder(id_routs, date1, date2, date3);
					request.setAttribute("orders", orders);
					id_routs = 0;
					id_check_order = null;
					date1 = "";
					date2 = "";
					date3 = "";
					input.reset(id_routs, id_check_order, date1, date2, date3);
			} else 	if (request.getParameter("Delete") != null){
				
				if(id_check_order !=null){
					orderService.delete(id_check_order);
					id_routs = 0;
					id_check_order = null;
					date1 = "";
					date2 = "";
					date3 = "";
					input.reset(id_routs, id_check_order, date1, date2, date3);
				}
			} else if (request.getParameter("All") !=null){
				List<IOrder> orders = orderService.getOrder();
		    	request.setAttribute("orders", orders);
			} else {
				List<IOrder> orders = orderService.getOrder();
		    	request.setAttribute("orders", orders);
			}
			
			List<IRouts> routs = routsService.getRouts();
			request.setAttribute("routs", routs);
	    	
		return mapping.findForward(ForwardActionTo.SUCCESS);
	}
}
